import http from './HTTP'
import api from './api'
import planning from '../utils/routeplanning'
//设置data
let params = {
  key: 'NMKBZ-SACK6-WFDSR-M7UUJ-Q7OYF-FQB6V',
  table_id: '60530e43edbe45292209e9f2',
  page_index: 1,
  page_size: 20,
}

function getMarkers() {
  let attractionsList = []
  // get请求获取地点数据
  http.get(api.url, params, res => {
    console.log('markers请求成功');
    let markers = []
    let markersArr = [{}]
    //解构sourceArr 批量赋值给markers[]
    for (let i = 0; i < res.data.result.data.length; i++) {
      //处理markers标点
      markers.push(
        ...markersArr.map(
          ({
            id,
            longitude,
            latitude,
            callout,
            width,
            height,
            iconPath,
            label,
          }) => ({
            id: i,
            longitude: res.data.result.data[i].location.lng,
            latitude: res.data.result.data[i].location.lat,
            callout: {
              content: `查看景点介绍`,
              color: '#1296db',
              fontSize: 12,
              borderRadius: 4,
              borderWidth: 1,
              borderColor: '#1296db',
              bgColor: '#ffffff',
              padding: 6,
              textAlign: 'center',
              display: 'BYCLICK', //'BYCLICK':点击显示; 'ALWAYS':常显
            },
            label: {
              content: res.data.result.data[i].title,
              color: '#F24E4E',
              fontSize: 12,
              borderRadius: 4,
              borderWidth: 1,
              borderColor: '#c23333',
              bgColor: '#ffffff90',
              padding: 6,
              anchorX: 10,
              anchorY: 0,
            },
            width: 30,
            height: 30,
            iconPath: '/images/标记.png',
          })
        )
      );
    }
    console.log(markers);
    for (const k in markers) {
      attractionsList.push(markers[k].label.content) //列表显示的文字
    }
    this.setData({
      markers: markers,
      list: attractionsList,
      pointimg: 'none', //景点导航图标默认隐藏
      areaimg: '', //景区导航图标默认显示
    })
  }, err => {
    console.log('错误信息,', err);
  })
}

function listplan(e) {
  let addressArr = [],
    cityArr = [],
    longitudeArr = [],
    latitudeArr = [],
    nameArr = [],
    idArr = [];
  let address, city, lon, lat, name;
  let Index = e.target.dataset.index; //获取点击的下标
  http.get(api.url, params, res => {
    //解构sourceArr 批量赋值给markers[]
    for (let i = 0; i < res.data.result.data.length; i++) {
      addressArr.push(res.data.result.data[i].address)
      cityArr.push(res.data.result.data[i].city)
      longitudeArr.push(res.data.result.data[i].location.lng)
      latitudeArr.push(res.data.result.data[i].location.lat)
      nameArr.push(res.data.result.data[i].title)
      idArr.push(res.data.result.data[i].ud_id) //获取的id是颠倒的
      if (Index == idArr[i] - 1) {
        address = addressArr[i]
        city = cityArr[i]
        lon = longitudeArr[i]
        lat = latitudeArr[i]
        name = nameArr[i]
      }
    }
    console.log(name);
    let endAddressInfo = {
      address: address,
      city: city,
      longitude: lon,
      latitude: lat,
      name: name,
    }
    planning.planning(endAddressInfo)
  }, err => {
    console.log('错误信息,', err);
  })
}
export default {
  getMarkers,
  listplan
}